package nc.impl.m4q.linkquery;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.trade.billsource.IBillFlow;
import nc.impl.pubapp.linkquery.MultiSrcBillFinder;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.pub.m4q.cons.DefFieldCons;
import nc.vo.pubapp.pattern.pub.SqlBuilder;
import nc.vo.trade.billsource.LightBillVO;

public class M4qSrcBillFinder extends MultiSrcBillFinder {

	@Override
	protected String createQueryFwdSql(IBillFlow billflow, String... srcBillID) {
		SqlBuilder sqlB = new SqlBuilder();
		sqlB.append(" select distinct ic_locadjust_h.vtrantypecode transtype, ");
		sqlB.append(" ic_locadjust_h.ctrantypeid transtypepk, ");
		sqlB.append(" 0 saga_status, ");
		sqlB.append(" ic_locadjust_h.cgeneralhid id, ");
		sqlB.append(" ic_locadjust_h.pk_org corp, ");
		sqlB.append(" ic_locadjust_h.vbillcode code, ");
		sqlB.append(" ic_locadjust_b."+DefFieldCons.M4Q_BODY_SRCBILLID+" sourceID ");
		sqlB.append(" from ic_locadjust_b ");
		sqlB.append(" left join ic_locadjust_h on ic_locadjust_h.cgeneralhid=ic_locadjust_b.cgeneralhid ");
		sqlB.append(" where ic_locadjust_b.dr=0 and ic_locadjust_b."+DefFieldCons.M4Q_BODY_SRCBILLTYPE+" in (?, ?) and ic_locadjust_b."+DefFieldCons.M4Q_BODY_SRCBILLID+" ", srcBillID);
		
		return sqlB.toString();
	}
	
	@Override
	public LightBillVO[] getSourceBills(String curBillType, String curBillID) {
		
		if(StringUtils.isNotEmpty(curBillID)) {
			BaseDAO dao = new BaseDAO();
			String sql = "SELECT DISTINCT "+DefFieldCons.M4Q_BODY_SRCBILLTYPE+" as type, "+DefFieldCons.M4Q_BODY_SRCBILLID+" id, "+DefFieldCons.M4Q_BODY_SRCBILLCODE+" as code  FROM ic_locadjust_b WHERE isnull("+DefFieldCons.M4Q_BODY_SRCBILLID+", '~') <> '~' and cgeneralhid = '"+curBillID+"'    and dr=0";
			
			try {
				List<LightBillVO> list = (List<LightBillVO>) dao.executeQuery(sql, new BeanListProcessor(LightBillVO.class));
				
				if(list != null && list.size() > 0) {
					LightBillVO[] vos = new LightBillVO[list.size()];
					return list.toArray(vos);
				}
			} catch (DAOException e) {
				e.printStackTrace();
			}
		}
		
		return null;
	}

}
